using Microsoft.AspNetCore.Mvc;
using ShopForWeb.Models;
using ShopForWeb.Service;
using ShopForWeb.Dto.ViewModels;
using ShopForWeb.Dto.AjaxModels;

namespace ShopForWeb.UI.Controllers;
[Route("testdb")]
public class TestDbController : Controller
{

  //注入
  private UserService _service;
  public TestDbController(UserService service)
  {
    _service = service;
  }
  /*
    [Route("attach")]
    public IActionResult TestAttach()
    {
      User u1 = new User()
      {
        UserID = 1,
        Name = "张三"
      };

      User u2 = new User()
      {
        UserID = 1,
        Name = "张三"
      };

      EntityEntry<User> e1 = _context.Attach<User>(u1);
      EntityEntry<User> e2 = _context.Attach<User>(u2);

      return View();
    }
*/
  [HttpPost, Route("add/1")]
  public async Task<IActionResult> TestAdd1(UserViewModels uvm)
  {
    await _service.AddUser1Async(uvm);
    ViewBag.Source = "TestAdd1";
    return View("~/Views/TestDb/TestAdd.cshtml");
  }

  [Route("select/1")]
  public async Task<IActionResult> TestSelect1()
  {
    var users = await _service.SelectUser1Async();
    ViewBag.Users = users;
    // ViewBag.Users = users;
    return View();
  }

  [Route("select/3")]
  public async Task<IActionResult> TestSelect3()
  {
    var users = await _service.SelectUser1Async();

    return Json(new ResponseAjaxModel()
    {
      Status = 0,
      Data = users
    });
  }



  /*
  [HttpPost, Route("add/2")]
  public async Task<IActionResult> TestAdd2(User u)
  {

    await _context.Users.AddAsync(u);
    await _context.SaveChangesAsync();
    ViewBag.Source = "TestAdd2";
    return View("~/Views/TestDb/TestAdd.cshtml");
  }
  [HttpPost, Route("upd/1")]
  public async Task<IActionResult> Testupd1(User u)
  {
    _context.Entry<User>(u).State = EntityState.Modified;
    await _context.SaveChangesAsync();
    ViewBag.Source = "Testupd1";
    return View("~/Views/TestDb/TestUpdate.cshtml");
  }
  [HttpPost, Route("upd/2")]
  public async Task<IActionResult> Testupd2(User u)
  {
    _context.Users.Update(u);
    await _context.SaveChangesAsync();
    ViewBag.Source = "Testupd2";
    return View("~/Views/TestDb/TestUpdate.cshtml");
  }
  [HttpGet, Route("del/1")]
  public async Task<IActionResult> Testdel1(User u)
  {
    var user = _context.Users.Find(u.UserID);//设置目标参数
    if (user != null)//非空
    {
      _context.Users.Remove(user);//删除操作
      await _context.SaveChangesAsync();//接收结果

    }
    ViewBag.Source = "Testdel1";
    return View("~/Views/TestDb/TestDelete.cshtml");
  }
  [HttpPost, Route("del/2")]
  public async Task<IActionResult> Testdel2(User u)
  {

    var o = _context.Entry(new User { UserID = 12 });
    o.State = EntityState.Deleted;
    await _context.SaveChangesAsync();
    ViewBag.Source = "Testdel2";
    return View("~/Views/TestDb/TestDelete.cshtml");
  }

  [HttpPost, Route("upd/3")]
  public async Task<IActionResult> Testupd3(User u)
  {
    _context.Entry<User>(u).Property("Password").IsModified = true;//可以单独修改
    await _context.SaveChangesAsync();
    ViewBag.Source = "Testupd3";
    return View("~/Views/TestDb/TestUpdate.cshtml");
  }

  [Route("select/1")]
  public async Task<IActionResult> TestSelect1()
  {
    ViewBag.Users = await _context.Users.Where(u => u.Sex == "男" && EF.Functions.Like(u.Name, "张%"))
                                    .AsNoTracking()
                                    .ToListAsync();

    // ViewBag.Users = users;
    return View();
  }

  [Route("select/2")]
  public async Task<IActionResult> TestSelect2()
  {
    ViewBag.Amount = await _context.Users.GroupBy(u => new { u.Sex })
                              .Select(g => new
                              {
                                Sex = g.Key.Sex,
                                Count = g.Count()
                              })
                            .ToListAsync();

    return View();
  }

*/
}
